﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using CoreLibs;
using HUGATE.ItemObject;
using HUGATE.Utility;

namespace MD_Asset.Controls.Systems
{
    public partial class SystemErrorManager : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                IData objData = Data.CreateData();

                try
                {
                    objData.Connect();
                    objData.CreateNewStoredProcedure("System_ErrorType_SelectAll");
                    ddlErrorType.DataSource = objData.ExecStoreToDataTable();
                    ddlErrorType.DataBind();
                    ddlErrorType.Items.Insert(0, new ListItem("--Loại lỗi--", "-1"));

                    txtDateFrom.SelectedDate = DateTime.Now;
                    txtDateTo.SelectedDate = DateTime.Now.AddDays(1);
                }
                catch(Exception objEx)
                {
                    //Session["error"] = new SystemMessage("Lỗi khi lấy dữ liệu!", Globals.ApplicationVRoot() + "/" + ErrorReturnPage, ex.Message);
                    //Session["error"] = new SystemError("Lỗi khi lấy dữ liệu!", objEx, Globals.ApplicationVRoot() + "/" + ErrorReturnPage);
                    //Response.Redirect(Globals.ApplicationVRoot() + "/" + MessagePage);
                }
                finally
                {
                    objData.Disconnect();
                }
                BindData(0);
            }
            //btnReturn.OnClientClick = "window.location='" + Globals.ApplicationVRoot() + "/" + ReturnPage + "';return true;";
        }

        private void BindData(int intPage)
        {
            IData objData = Data.CreateData();
            try
            {
                objData.Connect();
                objData.CreateNewStoredProcedure("System_ErrorLog_SelectAll");
                objData.AddParameter("@txtSearch", CoreLibs.Globals.FilterHtmlTab(txtSearch.Text));
                objData.AddParameter("@ErrorTypeID", ddlErrorType.SelectedValue);
                objData.AddParameter("@IsFix", !chkStatus.Checked);
                if (txtDateFrom.SelectedDate != null)
                    objData.AddParameter("@StartDate", txtDateFrom.SelectedDate.Value);
                if (txtDateTo.SelectedDate != null)
                    objData.AddParameter("@EndDate", txtDateTo.SelectedDate.Value);
                grvObjectList.DataSource = objData.ExecStoreToDataTable();
                grvObjectList.DataBind();
            }
            catch (Exception objEx)
            {
                //Session["error"] = new SystemError("Lỗi khi lấy dữ liệu!", objEx, Globals.ApplicationVRoot() + "/" + ErrorReturnPage);
                //Response.Redirect(Globals.ApplicationVRoot() + "/" + MessagePage);
            }
            finally
            {
                objData.Disconnect();
            }
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            IData objData = Data.CreateData();
            try
            {
                objData.Connect();

                string[] strSelect;
                if (Request["iptSelect"] != null)
                {
                    strSelect = Request["iptSelect"].Split(',');
                    for (int i = 0; i < strSelect.Length; i++)
                    {
                        objData.CreateNewStoredProcedure("System_ErrorLog_Delete");
                        objData.AddParameter("@ErrorID", strSelect[i]);
                        objData.AddParameter("@DeletedUser", "administrator");//User.UserName);
                        objData.ExecNonQuery();
                    }

                    GlobalFunction.ShowMessageBox(Page, "Xoá thành công.");
                    BindData(grvObjectList.PageIndex);
                }
            }
            catch (Exception objEx)
            {
                //Session["error"] = new SystemMessage("Lỗi khi xoá dữ liệu!", Globals.ApplicationVRoot() + "/" + ErrorReturnPage, ex.Message);
                //Session["error"] = new SystemError("Lỗi khi xoá dữ liệu!", objEx, Globals.ApplicationVRoot() + "/" + ErrorReturnPage);
                //Response.Redirect(Globals.ApplicationVRoot() + "/" + MessagePage);
            }
            finally
            {
                objData.Disconnect();
            }
        }

        protected void btnFix_Click(object sender, EventArgs e)
        {
            IData objData = Data.CreateData();
            try
            {
                objData.Connect();
                string[] strSelect;
                if (Request["iptSelect"] != null)
                {
                    strSelect = Request["iptSelect"].Split(',');
                    for (int i = 0; i < strSelect.Length; i++)
                    {
                        objData.CreateNewStoredProcedure("System_ErrorLog_Update");
                        objData.AddParameter("@ErrorID", strSelect[i]);
                        objData.AddParameter("@UpdatedUser", "administrator");//User.UserName);
                        objData.ExecNonQuery();
                    }
                    GlobalFunction.ShowMessageBox(Page, "Cập nhật thành công.");
                    BindData(grvObjectList.PageIndex);
                }
            }
            catch (Exception ex)
            {
                //Session["error"] = new SystemMessage("Lỗi khi cập nhật xoá dữ liệu!", Globals.ApplicationVRoot() + "/" + ErrorReturnPage, ex.Message);
                //Session["error"] = new SystemError("Lỗi khi cập nhật dữ liệu!", ex, Globals.ApplicationVRoot() + "/" + ErrorReturnPage);
                //Response.Redirect(Globals.ApplicationVRoot() + "/" + MessagePage);
            }
            finally
            {
                objData.Disconnect();
            }
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            BindData(0);
        }

        protected void grvObjectList_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            BindData(e.NewPageIndex);
        }

    }
}